﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.gates {
    import flash.events.*;
    import Engine.Managers.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import Display.*;
    import Modules.stats.experiments.*;
    import Modules.crew.*;
    import Display.GateUI.*;

    public class CrewGate extends AbstractGate {

        protected static const REQUIRED_CREW:String = "required_crew";

        public function CrewGate(_arg1:String){
            super(_arg1);
        }
        public static function getCrewCost(_arg1:String, _arg2:String=null):Number{
            var gates:* = null;
            var key:* = null;
            var keyName:* = null;
            var itemName:* = _arg1;
            var gateName = _arg2;
            var result:* = Global.gameSettings.getNumber("crewMemberCashCost");
            var item:* = Global.gameSettings.getItemByName(itemName);
            if (gateName == null){
                gates = item.gatesXml["gate"]["key"];
            } else {
                gates = item.gatesXml["gate"].(attribute("name") == gateName)["key"];
            };
            for each (key in gates) {
                keyName = key.@name;
                if (keyName == REQUIRED_CREW){
                    if (key.hasOwnProperty("@cashCost")){
                        result = Number(key.@cashCost);
                        break;
                    };
                };
            };
            return (result);
        }
        public static function getFilledPositions(_arg1:Array):Number{
            var _local3:Object;
            var _local2:Number = 0;
            for each (_local3 in _arg1) {
                _local2 = (_local2 + _local3.count);
            };
            return (_local2);
        }

        override public function checkForKeys():Boolean{
            var _local1:Boolean = super.checkForKeys();
            var _local2:int = Global.player.inventory.getItemCountByName("crew_bonus");
            var _local3:Crew = Global.crews.getCrewById(m_targetObjectId);
            var _local4:Number = m_keys[REQUIRED_CREW];
            if (_local3){
                _local1 = ((_local3.count + _local2) >= _local4);
            } else {
                if (_local2 >= _local4){
                    _local1 = true;
                };
            };
            return (_local1);
        }
        override protected function takeKeys():void{
            var _local5:GameObject;
            var _local1:int = Global.player.inventory.getItemCountByName("crew_bonus");
            var _local2:Crew = Global.crews.getCrewById(m_targetObjectId);
            var _local3:Number = m_keys[REQUIRED_CREW];
            var _local4:int;
            if (_local2){
                _local4 = (_local3 - _local2.count);
            } else {
                if (_local1 >= _local3){
                    _local4 = _local3;
                };
            };
            Global.player.inventory.removeItems("crew_bonus", _local4);
            while (_local4 > 0) {
                Global.crews.addCrewById(m_targetObjectId, "-1");
                _local5 = (Global.world.getObjectById(m_targetObjectId) as GameObject);
                if (_local5){
                    _local5.onAddCrewMember();
                };
                _local4--;
            };
        }
        override public function getRequirementString():String{
            return (null);
        }
        override public function displayGate(_arg1:String=null, _arg2:String=null):void{
            var _local12:String;
            var _local13:Object;
            var _local14:Player;
            var _local15:Player;
            m_dialog = null;
            var _local3:Array = new Array();
            var _local4:Crew = Global.crews.getCrewById(m_targetObjectId);
            var _local5:Vector.<String> = this.getCrewNames();
            var _local6:int = Global.player.inventory.getItemCountByName("crew_bonus");
            var _local7:Number = m_keys[REQUIRED_CREW];
            var _local8:int;
            while (_local8 < _local7) {
                _local12 = (((_local4) && ((_local4.count > _local8)))) ? _local4.list[_local8] : null;
                _local12 = this.normalizeUid(_local12);
                _local13 = new Object();
                _local14 = (_local12) ? Global.player.findFriendById(_local12) : null;
                _local15 = Global.player.findFriendById("-1");
                if (_local12){
                    _local13.url = (_local14) ? _local14.snUser.picture : _local15.snUser.picture;
                    _local13.count = 1;
                    _local13.amountNeeded = 1;
                    _local13.footerText = (_local14) ? _local14.firstName : _local15.firstName;
                } else {
                    if (_local6 > 0){
                        _local13.url = _local15.snUser.picture;
                        _local13.count = 1;
                        _local13.amountNeeded = 1;
                        _local13.footerText = ZLoc.t("Items", "crew_bonus_friendlyName");
                        _local6--;
                    } else {
                        _local13.url = null;
                        _local13.displayObjectClass = EmbeddedArt.hud_no_profile_pic;
                        _local13.cost = getCrewCost(m_unlockItemName);
                        _local13.count = 0;
                        _local13.amountNeeded = 1;
                        _local13.buyCallback = this.buyCrew;
                        _local13.footerText = ZLoc.t("Dialogs", "EmptyCrewCell");
                    };
                };
                _local13.picUrl = _local13.url;
                _local13.friendName = _local13.footerText;
                _local13.name = _local5[_local8];
                _local13.backgroundClass = EmbeddedArt.crewFriendBackground;
                _local3.push(_local13);
                _local8++;
            };
            m_cellFactory = new KeyCellCrewFactory(_local3);
            var _local9:Item = Global.gameSettings.getItemByName(m_unlockItemName);
            var _local10:String = ZLoc.t("Dialogs", "CrewGateDialogTitle", {item:_local9.localizedName});
            if (_local9.name == "warehouse_deluxe"){
                m_dialog = new CrewGateDialog_2(this.getInstructions(), _local3, m_cellFactory, _local10, this.getRequirementString(), this.checkForKeys, this.gateFinishHandler, 4, 155);
            } else {
                m_dialog = new CrewGateDialog(this.getInstructions(), _local3, m_cellFactory, _local10, this.getRequirementString(), this.checkForKeys, this.gateFinishHandler, 4, 155);
            };
            m_dialog.addEventListener("friendClicked", this.gateFinishHandler, false, 0, true);
            UI.displayPopup(m_dialog);
            var _local11:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CREW_POPUP_MFS);
            if (_local11 == ExperimentDefinitions.TRAY_POPUP_MFS){
                FrameManager.preloadTray(("crew.php?mId=" + m_targetObjectId.toString()), true);
            };
        }
        protected function normalizeUid(_arg1:String):String{
            var _local3:Number;
            var _local2:String = _arg1;
            if (_arg1){
                _local3 = parseFloat(_arg1);
                if (_local3 < 0){
                    _local3 = -1;
                };
                _local2 = _local3.toString();
            };
            return (_local2);
        }
        protected function gateFinishHandler(_arg1:Event=null):void{
            if (this.checkForKeys()){
                unlockGate();
                m_dialog.close();
            } else {
                this.askForCrew();
            };
        }
        protected function askForCrew(_arg1:String=null):void{
            var _local2:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CREW_POPUP_MFS);
            var _local3:String = ("crew.php?mId=" + m_targetObjectId.toString());
            if (((!((_arg1 == null))) && (!((_arg1 == ""))))){
                _local3 = (_local3 + ("&gname=" + _arg1));
            };
            if (_local2 == ExperimentDefinitions.TRAY_POPUP_MFS){
                FrameManager.showTray(_local3);
                (m_dialog as GateDialog).close();
            } else {
                FrameManager.navigateTo(_local3);
            };
        }
        protected function buyCrew(_arg1:Number):Boolean{
            StatsManager.count("crewBuilding", "buy", m_unlockItemName, _arg1.toString());
            var _local2:Boolean;
            if (Global.player.canBuyCash(_arg1, true)){
                _local2 = true;
                GameTransactionManager.addTransaction(new TBuyCrew(m_targetObjectId, 0, _arg1));
                if (this.checkForKeys() == true){
                    (m_dialog as GateDialog).activateFinishButton();
                };
            };
            updateItemArrowState();
            return (_local2);
        }
        protected function getCrewNames():Vector.<String>{
            var _local3:XML;
            var _local4:String;
            var _local5:Number;
            var _local6:String;
            var _local7:int;
            var _local1:Vector.<String> = new Vector.<String>();
            var _local2:Item = Global.gameSettings.getItemByName(m_unlockItemName);
            for each (_local3 in _local2.gatesXml["gate"]["key"]["member"]) {
                _local4 = _local3.@name;
                _local5 = _local3.@amount;
                _local6 = ZLoc.t("Items", _local4);
                _local7 = 0;
                while (_local7 < _local5) {
                    _local1.push(_local6);
                    _local7++;
                };
            };
            return (_local1);
        }
        protected function getInstructions():String{
            var _local3:XML;
            var _local1 = "";
            var _local2:Item = Global.gameSettings.getItemByName(m_unlockItemName);
            for each (_local3 in _local2.gatesXml["gate"]) {
                if (((_local3) && ((_local3.@instructions == true)))){
                    _local1 = ZLoc.t("Dialogs", "crewInstructionsLine1", {item:_local2.localizedName});
                    _local1 = (_local1 + ("\n" + ZLoc.t("Dialogs", "crewInstructionsLine2", {item:_local2.localizedName})));
                };
            };
            return (_local1);
        }

    }
}//package Classes.gates 
